package com.xuxingcan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by xuxingcan on 2/22/16.
 */
public class HttpUtil {
    /**
     * 解析地址格式
     */
    private static String prisePath = "http://www.flvcd.com/parse.php?kw=http://v.yinyuetai.com/video/2501352?f=SY-MKDT-MVSB-1&format=super";

    public static String getFlvcdUrl(String path) {
        return "http://www.flvcd.com/parse.php?kw=" + path + "&format=super";
    }

    public static String get(String url, String charsetName) {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setConnectTimeout(10 * 1000);
            conn.setReadTimeout(20 * 1000);
            conn.setRequestMethod("GET");
            InputStream in = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, charsetName));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            in.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return null;
    }
}
